package com.vanky.graph.service.impl;

import com.vanky.graph.pojo.Vertex;
import com.vanky.graph.pojo.fqResult;
import com.vanky.graph.properties.HugeClientProperties;
import com.vanky.graph.service.FuzzyQueryService;
import com.vanky.graph.utils.JsonUtil;
import org.apache.hugegraph.driver.GremlinManager;
import org.apache.hugegraph.driver.HugeClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class FuzzyQueryServiceImpl implements FuzzyQueryService {

    @Autowired
    HugeClientProperties hugeClientProperties;

    /*
     * 模糊查询指定标签的节点,用于列表展示
     */
    public List<Vertex> FuzzyQueryListByLabel(String word, String label, String property){
        //连接到 HugeGraph 数据库
        HugeClient hugeClient = HugeClient.builder(hugeClientProperties.getUrl(),hugeClientProperties.getGraph())
                .build();

        ////获取 gremlin 管理器
        GremlinManager gremlin = hugeClient.gremlin();

        //执行gremlin语句进行查询
        List<Object> data = gremlin.gremlin("g.V().hasLabel('"+label+"').filter{it.get().property('"+property+"').value()==~'.*"+word+".*'}").execute().data();

        //关闭服务器连接
        hugeClient.close();

        //返回json格式的结果
        return JsonUtil.toJson(data, Vertex.class);
    }



    /*
     * 模糊查询所有节点,用于列表展示
     */
    @Override
    public List<Vertex> FuzzyQueryListAll(String word){

        //连接到 HugeGraph 数据库
        HugeClient hugeClient = HugeClient.builder(hugeClientProperties.getUrl(),hugeClientProperties.getGraph())
                .build();

        //获取 gremlin 管理器
        GremlinManager gremlin = hugeClient.gremlin();

        //执行gremlin语句进行查询
        List<Object> data = gremlin.gremlin("g.V().where(__.properties().hasValue(Text.contains('"+word+"')))").execute().data();

        //关闭服务器连接
        hugeClient.close();

        //返回json格式的结果
        return JsonUtil.toJson(data, Vertex.class);
    }

    /*
     * 模糊查询法庭节点,用于列表展示
     */
    @Override
    public List<Vertex> FuzzyQueryListCourt(String word){
        List<Vertex> results = FuzzyQueryListByLabel(word,"slice_court","court_content");
        return results;
    }

    /*
     * 模糊查询法官节点,用于列表展示
     */
    @Override
    public List<Vertex> FuzzyQueryListJudge(String word){
        List<Vertex> results = FuzzyQueryListByLabel(word,"slice_judge","judge_content");
        return results;
    }

    /*
     * 模糊查询案由节点,用于列表展示
     */
    @Override
    public List<Vertex> FuzzyQueryListCauseType(String word){
        List<Vertex> results = FuzzyQueryListByLabel(word,"slice_cause","cause_type");
        return results;
    }

    /*
     * 模糊查询法条节点,用于列表展示
     */
    @Override
    public List<Vertex> FuzzyQueryListLaw(String word){
        List<Vertex> results = FuzzyQueryListByLabel(word,"slice_law","law_content");
        return results;
    }

    /*
     * 模糊查询律所节点,用于列表展示
     */
    @Override
    public List<Vertex> FuzzyQueryListLawFirm(String word){
        List<Vertex> results = FuzzyQueryListByLabel(word,"slice_lawfirm","lawfirm_content");
        return results;
    }

    /*
     * 模糊查询律师节点,用于列表展示
     */
    @Override
    public List<Vertex> FuzzyQueryListLawyer(String word){
        List<Vertex> results = FuzzyQueryListByLabel(word,"slice_lawyer","lawyer_content");
        return results;
    }

    /*
     * 模糊查询当事人节点,用于列表展示
     */
    @Override
    public List<Vertex> FuzzyQueryListLitigant(String word){
        List<Vertex> results = FuzzyQueryListByLabel(word,"slice_litigant","litigant_content");
        return results;
    }

    /*
     * 模糊查询案件节点,用于列表展示
     */
    @Override
    public List<Vertex> FuzzyQueryListCase(String word){
        List<Vertex> results = FuzzyQueryListByLabel(word,"slice_case","case_name");
        return results;
    }

    /*
     * 根据id模糊查询法院,及相连的两层节点
     */
    @Override
    public List<fqResult> FuzzyQueryById(String id){

        //连接到 HugeGraph 数据库
        HugeClient hugeClient = HugeClient.builder(hugeClientProperties.getUrl(),hugeClientProperties.getGraph())
                .build();

        //获取 gremlin 管理器
        GremlinManager gremlin = hugeClient.gremlin();

        //执行gremlin语句进行查询
        List<Object> data = gremlin.gremlin("g.V().hasId('"+id+"').as('startNode')\n" +
                "  .bothE().as('edgeToMiddle').otherV().as('middleNode')\n" +
                "  .bothE().as('edgeToEnd').otherV().as('endNode')\n" +
                "  .select('startNode','edgeToMiddle','middleNode','edgeToEnd','endNode')").execute().data();

        //关闭服务器连接
        hugeClient.close();

        //返回json格式的结果
        return JsonUtil.toJson(data, fqResult.class);
    }
}
